/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	         </p>
 * <p>创建者   :	heyong 
 * 
 * <p>描   述  :   provinceController.java for com.pengtu.gsj.controller.checker    </p>
 * 
 * <p>最后修改 : $: 2017年7月4日-上午10:21:11 v 1.0.0	 win7   $ </p>
 * 
*/

package com.pengtu.gsj.controller.checker;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.pengtu.gsj.controller.base.CheckBaseController;
import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.entity.app.Sysmenu;
import com.pengtu.gsj.entity.check.CheckProject;
import com.pengtu.gsj.interceptor.Token;
import com.pengtu.gsj.service.check.CheckProjectService;
import com.pengtu.gsj.service.check.ProvinceService;
import com.pengtu.gsj.service.system.SysmenuManagerService;
import com.pengtu.gsj.utils.web.SpringMvcUtils;

/**
 * 
 * ProvinceController
 * 
 * 2017年7月4日 上午10:21:11
 * 
 * @version 1.0.0
 * 
 */
@Controller
@RequestMapping("province")
public class ProvinceController extends CheckBaseController {
	
	@Autowired
	private SysmenuManagerService sysmenuManagerService;
	
	@Autowired
	private ProvinceService provinceService;
	@Autowired
	private CheckProjectService checkProjectService;
	
	
	/**
	 * 
	 * loadEntity:
	 * 适用:加载国省检项目实体类
	 * @param id
	 * @param model 
	 * @exception 
	 * @since  1.0.0
	 */
	@ModelAttribute
	public void loadEntity(String id, Model model){
		CheckProject checkProject ;
		if (StringUtils.isNotEmpty(id)) {
			checkProject = provinceService.getCheckProject(id);
		}else {
			checkProject = new CheckProject();
		}
		model.addAttribute("checkProject", checkProject);
		model.addAttribute("parentId", SpringMvcUtils.getParameter("parentId"));
		model.addAttribute("left", SpringMvcUtils.getParameter("left"));
		model.addAttribute("type", SpringMvcUtils.getParameter("type"));
	}
	
	
	/**
	 * 
	 * showList:
	 * 适用:跳转到国省列表页面
	 * @param model
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("showList")
	public String showList(Model model) {
		//判断系统中国检是否有未发布的项目  如果有 则不新增
		//true 表示没有  false  表示有
		if (checkProjectService.getStatusCheckProjects("1")) {
			model.addAttribute("add", true);
		} else {
			model.addAttribute("add", false);
		}
		return "check/provinceCheck";
	}
	
	
	
	/**
	 * 
	 * getCheckProjectList:
	 * 适用:获取国省数据列表
	 * @param page  接受page相关的数据
	 * @param request
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping(value="list", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Page<CheckProject> getCheckProjectList(Page<CheckProject> page, HttpServletRequest request, Model model){
		String msg = SpringMvcUtils.getParameter("msg");
		if (StringUtils.isNotEmpty(msg)) {
			model.addAttribute("msg", msg);
		}
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(request);
		if (!page.isOrderBySetted()) {
			page.setOrderBy("createDate");
			page.setOrder(Page.DESC);
		}
		page = provinceService.getCheckProjects(page, queryFilter);
		return page;
	}
	
	
	/**
	 * 
	 * getMenus:根据菜单id，获取菜单以及父级菜单
	 * 适用:
	 * @param request
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping(value="getMenus", produces = "application/json; charset=utf-8")
	@ResponseBody
	public List<Sysmenu> getMenus(HttpServletRequest request){
		
		List<Sysmenu> sysmenus = new ArrayList<Sysmenu>();
		String menuId = SpringMvcUtils.getParameter("id1");
		if (menuId != null && menuId.length() > 0) {
			sysmenus = sysmenuManagerService.getParentMenusById(menuId);
		}
		return sysmenus;
	}
	
	
	/**
	 * 
	 * showOrInput:
	 * 适用:跳转到新增国省项目页面或者是修改国检项目信息页面
	 * @param checkProject
	 * @param model
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("input")
	@Token(save = true)
	public String showOrInput(@ModelAttribute CheckProject checkProject, Model model) {
		//获取所有日常监管菜单中的业务模块菜单
		List<Sysmenu> menuList =sysmenuManagerService.getcheckSysmenus();
		model.addAttribute("menuList", menuList);
		return "check/provinceProject_input";
	}
	
	
	
	/**
	 * 
	 * saveCheckProject:
	 * 适用:保存省检项目信息
	 * @param checkProject
	 * @param attributes
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("save")
	@Token(remove = true)
	public String saveCheckProject(@ModelAttribute CheckProject checkProject,RedirectAttributes attributes)throws Exception{
		//保存时   得到检查单中最小的菜单的检查连接和各位置信息
		//首先得到公路监管下十大模块的id集合
		List<String> list = sysmenuManagerService.getIdStrByParentId("402880f65b8f1cea015b8f24ae3d0001");
		String  showId = "";
		String childId = "";
		String parentId = "";
		if (checkProject.getMenuListSize()>0) {
			for (Sysmenu sysmenu : checkProject.getMenuList()) {
				//确定那个大模块中有选中的菜单
				if (list.contains(sysmenu.getId())) {
					showId = sysmenu.getId();
					break;
				}
			}
			//找到该模块下 按照添加时间排序的子模块
			List<String> childIds = sysmenuManagerService.getMenuByParentIds(showId);
			for (Sysmenu sysmenu : checkProject.getMenuList()) {
				if (childIds.contains(sysmenu.getId())) {
					childId = sysmenu.getId();
					Sysmenu parentMenu = sysmenuManagerService.get(childId);
					parentId = parentMenu.getParent().getId();
					
					//如果子模块的父菜单id与showId一样 则表示该模块只有2级菜单   否则有三级
					if (showId.equals(parentId)) {
						parentId = childId;
					}
					StringBuffer href = new StringBuffer();
					href.append(parentMenu.getCheckHref());
					if (parentMenu.getCheckHref().indexOf("?") > -1) {
						href.append("&");
					} else {
						href.append("?");
					}
					href.append("left=" + showId + "&leftChild=" + parentId + "&menuId=" + childId);
					System.out.println("href:"+href.toString());
					checkProject.setShowHref(href.toString());
					break;
				}
			}
			//判断该菜单是几级菜单 
		}
		provinceService.save(checkProject);
		attributes.addFlashAttribute("msg", "项目保存/更新成功！");
		attributes.addAttribute("parentId",	SpringMvcUtils.getParameter("parentId"));
		attributes.addAttribute("left", SpringMvcUtils.getParameter("left"));
		return "redirect:/province/showList.do";
	}
}
